//道路选择

//调用数据接口处理业务逻辑数据

$(document).ready(function() {
	layui.use(['element', 'layer', 'util', 'form'], function (exports) {
		var $ = layui.jquery
			, layer = layui.layer
			, form = layui.form;
		form.on('select(roadSelect)', function(data){
			console.log('道路选择');
			$("#roadSeletionSelect option:not(:first)").remove();
			var roadID = $("#roadSelect").val();
			var promise = new Promise(function(reslove, reject) {
				$.ajax('https://entrywbzy4-vip.app.jtzny.com/dlssxx/main/daoluldxx/loadroadsections', {
					data: {
						roadID: roadID
					},
					dataType: 'json', //服务器返回json格式数据
					type: 'get', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						debugger
						var _op = "";
						for (let i = 0; i < data.length; i++) {
							_op += "<option value='" + data[i].fid + "'>" + data[i].fRoadSectionName + "</option>"
						}
						$("#roadSeletionSelect").append(_op);
						reslove(data);
					},
					error: function(xhr, type, errorThrown) {
						reject(error);
					}
				});
			});
			//将加载的数据显示到列表中
			promise.then(function(data) {
				listPlay(data, "路段");
			})
			return promise;
		});
		$("#btnRoad").click(function() {
			//$("#contenttbl").fadeToggle();//渐入效果
			$("#contenttbl").slideToggle("slow"); //上下伸缩效果

		});
		roadData().then(function(data) {
			listPlay(data, "道路");
		});
		roadAssetClass();
		// $("#dataListPlay li").click(function(){
		// 	var xx=$(this).text();
		// 	alert(xx);
		// })
	})

	$("#btnRoad").click(function() {
		//$("#contenttbl").fadeToggle();//渐入效果
		$("#contenttbl").slideToggle("slow"); //上下伸缩效果

	});
	roadData().then(function(data) {
		listPlay(data, "道路");
	});
	roadAssetClass();
	// $("#dataListPlay li").click(function(){
	// 	var xx=$(this).text();
	// 	alert(xx);
	// })

});

//道路选择
function roadData() {
	var promise1 = new Promise(function(reslove, reject) {
		$.ajax(baseUrl + 'dlssxx/main/daoluxx/loadallroads', {
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				debugger
				var _op = "";
				for (let i = 0; i < data.length; i++) {
					_op += "<option value='" + data[i].fid + "'>" + data[i].fRoadName + "</option>"
				}
				$("#roadSelect").append(_op);
				layui.form.render("select");
				reslove(data);
			},
			error: function(xhr, type, errorThrown) {
				reject(error);
			}
		});
	})
	return promise1;
}
//路段选择
function roadselectByRoadID() {
	$("#roadSeletionSelect option:not(:first)").remove();
	var roadID = $("#roadSelect").val();
	var promise = new Promise(function(reslove, reject) {
		$.ajax(baseUrl + 'dlssxx/main/daoluldxx/loadroadsections', {
			data: {
				roadID: roadID
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				debugger
				var _op = "";
				for (let i = 0; i < data.length; i++) {
					_op += "<option value='" + data[i].fid + "'>" + data[i].fRoadSectionName + "</option>"
				}
				$("#roadSeletionSelect").append(_op);
				reslove(data);
			},
			error: function(xhr, type, errorThrown) {
				reject(error);
			}
		});
	});
	//将加载的数据显示到列表中
	promise.then(function(data) {
		listPlay(data, "路段");
	})
	return promise;
}
//其他设施类型
function roadAssetClass() {

	var promise = new Promise(function(reslove, reject) {
		$.ajax(baseUrl + 'dlssxx/main/roadassetclassdict/loadallroadassetclassdicts', {
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				debugger
				var _op = "";
				for (let i = 0; i < data.length; i++) {
					_op += "<option value='" + data[i].fid + "'>" + data[i].fClassName + "</option>"
				}
				$("#roadAssetClass").append(_op);
				reslove(data);
			},
			error: function(xhr, type, errorThrown) {
				reject(errorThrown);
			}
		});
	})

	return promise;
}
//通过其他设施分类查找道路的附属设备信息，当编码类型为2001时，则请求的桥梁表，当为2002时为隧道表，否则为道路附属设施表
function roadAssetDetailByClass() {
	debugger
	var roadsectionID = $("#roadSeletionSelect").val();
	var roadAssetClassID = $("#roadAssetClass").val();
	//桥梁
	if (roadAssetClassID === "2001") {
		var promise = new Promise(function(reslove, reject) {
			$.ajax(baseUrl + 'dlssxx/main/qiaoliang/bridgeinfobyroadsectionid', {
				data: {
					roadsectionid: roadsectionID
				},
				dataType: 'json', //服务器返回json格式数据
				type: 'get', //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				success: function(data) {
					reslove(data);
				},
				error: function(xhr, type, errorThrown) {
					reject(error);
				}
			});
		});

		promise.then(function(data) {
			listPlay(data, '桥梁');
		})

	} else if (roadAssetClassID === "2002") { //隧道信息服务
		var promise = new Promise(function(reslove, reject) {
			$.ajax(baseUrl + 'dlssxx/main/suidao/tunnelinfobyroadsectionid', {
				data: {
					id: roadsectionID
				},
				dataType: 'json', //服务器返回json格式数据
				type: 'get', //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				success: function(data) {
					reslove(data);
				},
				error: function(xhr, type, errorThrown) {
					reject(error);
				}
			});
		});

		promise.then(function(data) {
			listPlay(data, '隧道');
		})
	} else {
		$.get(baseUrl + 'dlssxx/main/daolufsssgl/loadallroadincludeassetsbysectionid', {
			roadSectionID: roadsectionID,
			assetClassDictID: roadAssetClassID
		}, function(data) {
			listPlay(data, '道路附属设施');
		})

	}

}
window.selectModeCompentID = '';
//根据上面条件的选择，像是上面选择的详细信息
function listPlay(data, dboname) {
	debugger
	$("#dataListPlay").empty();
	var _li = "";
	for (let i = 0; i < data.length; i++) {
		if (dboname === "道路")
			_li += "<li id='" + data[i].ModeCompentID + "' >" + data[i].fRoadName + "</li>"
		else if (dboname === '路段')
			_li += "<li id='" + data[i].ModeCompentID + "' >" + data[i].fRoadSectionName + "</li>"
		else if (dboname === '桥梁')
			_li += "<li id='" + data[i].ModeCompentID + "' >" + data[i].fBridgeName + "</li>"
		else if (dboname === '隧道')
			_li += "<li id='" + data[i].ModeCompentID + "'  >" + data[i].fBridgeName + "</li>"
		else if (dboname === '道路附属设施')
			_li += "<li id='" + data[i].ModeCompentID + "'  >" + data[i].AssetCode + "</li>"
	}
	$("#dataListPlay").append(_li);
	$("#dataListPlay").unbind('click').click(function(event) {
		$(event.target).siblings('li').removeClass('selected');
		$(event.target).addClass('selected');
		var ModeCompentID = $(event.target).attr("id");
		selectModeCompentID = ModeCompentID;
		var assetName = $(event.target).text();
		if(ModeCompentID==null||ModeCompentID==undefined) return;
		let result = currentProject.findBIMComponentsByKeywords({ keyword: ModeCompentID });
		result.then(data => {
			let list = data.list;
			list.forEach(function(component) {
				debugger
				viewer.flyTo(component);
				component.select();
			})
		})
		//alert("测试" + ModeCompentID + ':' + assetName);
	});
}

//列表点击事件
// function liClick(e){
// 	var ModeCompentID=$("#dataListPlay li").text();
// 	var assetName=$("#dataListPlay li").name;
// 	alert("测试"+ModeCompentID+':'+assetName);
// }
